package handler

import (
	"context"
	"errors"
	"online_retaliers_server/basic/dao"
	"online_retaliers_server/basic/model"
	__ "online_retaliers_server/basic/proto"
)

type Server struct {
	__.UnimplementedOnlineRetailersServerServer
}

func (s *Server) GoodListOne(_ context.Context, req *__.GoodListOneReq) (*__.GoodListOneResp, error) {
	var good model.Good
	err := dao.GoodListOne(req.GoodId, &good)
	if err != true {
		return nil, errors.New("查询失败")
	}
	goodlistone := []*__.GoodListOne{}
	goodlistone = append(goodlistone, &__.GoodListOne{
		Name:     good.Name,
		Sum:      int64(good.Sum),
		GoodType: good.GoodType,
		Price:    float32(good.Price),
		Id:       int64(good.Id),
	})
	return &__.GoodListOneResp{ListOne: goodlistone}, nil
}
func (s *Server) OrderList(_ context.Context, req *__.OrderListReq) (*__.OrderListResp, error) {
	var order []model.Order
	err := dao.OrderList(&order)
	if err != true {
		return nil, errors.New("查询失败")
	}
	orderlists := []*__.OrderList{}
	for _, orderlist := range order {
		orderlists = append(orderlists, &__.OrderList{
			UserId:      int64(orderlist.UserId),
			GoodId:      int64(orderlist.GoodId),
			OrderNumber: orderlist.OrderNumber,
		})
	}

	return &__.OrderListResp{List: orderlists}, nil
}
func (s *Server) OrderAdd(_ context.Context, req *__.OrderAddReq) (*__.OrderAddResp, error) {
	order := model.Order{

		UserId:      uint(req.UserId),
		GoodId:      uint(req.GoodId),
		OrderNumber: req.OrderNumber,
	}

	err := dao.OrderAdd(&order)
	if err != true {
		return nil, errors.New("添加失败")
	}
	return &__.OrderAddResp{OrderId: int64(order.Id)}, nil
}
